<?php

namespace App\Http\Controllers\Admin;

use App\Model\Adress;
use App\Model\Order;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrderController extends CommonController
{
    public function index(Request $request)
    {
        $map = [];
        //订单状态
        if (!empty($orderstatus = $request->orderstatus)) {
            $map[] = ['orderstatus', '=', "{$orderstatus}"];
        }

        //定单号
        if (!empty($keyword = $request->keyword)) {
            $map[] = ['no', 'like', "%{$keyword}%"];
        }
        $orderDatas = Order::where($map)->orderBy('id', 'desc')->paginate(5);
        return view('admin.order.index', compact('orderDatas'));
    }


    //订单详情
    public function orderdetails($id)
    {
        //订单详情
        $order = Order::find($id);

        //订单收货地址
        $address = Adress::find($order['addrid']);

        return view('admin.order.orderdetails', compact('order', 'address'));
    }


    //更新订单状态
    public function changeorder($id, $status)
    {
        $order = Order::find($id);
        $order->orderstatus = $status;
        $order->save();
        return redirect('/admin/orderdetails/' . $id)->with('status', '操作成功');
    }

}
